import { ElMessage } from 'element-plus';
import { omit, pick } from 'lodash';

export function formatStr2Obj(item) {
	// console.log(item.replace(/\\/g, ''))
	const ret = JSON.parse(item.replace(/\\/g, ''));
	if (!ret && ret === '') {
		ElMessage({
			message: '886000:处理数据出错',
			type: 'error',
			duration: BASE_API.MSG_SHOW_TIME,
		});
		console.log('886000: ', item);
		return null;
	} else {
		return JSON.parse(item.replace(/\\/g, ''));
	}
}

export function treeData2Optioins(res, out) {
	if (!res) {
		return;
	}

	for (let i = 0; i < res.length; i++) {
		if (res[i].Child && res[i].Child.length > 0) {
			let item = { value: res[i].ItemCode, label: res[i].Title, children: [] };
			out.push(item);
			treeData2Optioins(res[i].Child, item.children);
		} else {
			out.push({
				value: res[i].ItemCode,
				label: res[i].Title,
			});
		}
	}
}

export function formatPageInfo(pageInfo) {
	// console.log('1-----',pageInfo.MainParams)
	// console.log(JSON.parse(pageInfo.MainParams))
	if (!pageInfo) {
		console.log('page tools.formatPageInfo pageInfo null, params: ', pageInfo);
		return null;
	}
	const params = formatStr2Obj(pageInfo.MainParams);
	if (!params) {
		console.log('page tools.formatPageInfo call formatStr2Obj return null, params: ', pageInfo.MainParams);
		return null;
	}
	// console.log('2-----',params)
	const globelParams = {};
	params.globelParams.map((p) => {
		// console.log('p.pValue', p)
		globelParams[p.name] = p.value;
	});
	console.log('globelParamsglobelParamsglobelParamsglobelParams', globelParams, params.globelParams);
	return {
		attrs: {
			// 先从参数中排除 'eventList', 'ifRefresh', 'actions', 'disabled' 下面这4个会特殊处理
			...omit(params, ['eventList', 'ifRefresh', 'actions', 'globelParams']),
			labelWidth: params.labelWidth === null || params.labelWidth === undefined ? null : params.labelWidth + 'px',
		},
		events: params['eventList'],
		others: {
			...pick(params, ['ifRefresh', 'actions']),
			globelParams,
		},
	};
}

export function formatPageCpn(cpn) {
	// console.log('formatPageCpn 0', cpn.MainParams)
	const params = formatStr2Obj(cpn.MainParams);
	if (!params) {
		console.error('[Error][CustomPage]Cpn no config: ', cpn);
		return null;
	}

	if (!params.tag || !params.formId) {
		console.error('[Error][CustomPage]Cpn is invalid, tag or formId undefined: ', cpn);
		return null;
	}

	const res = {
		sort: Number(cpn.Sort),

		attrs: {
			...omit(params, [
				'tag',
				'execTag',
				'regList',
				'fieldId',
				'formId',
				'PFormID',
				'vModel',
				'defaultValue',
				'ifStaticOption',
				'options',
				'dynamicOptions',
				'actions',
				'actionsCfg',
				'vModel_t',
				'vModel_l',
				'vModel_c',
			]),
			labelWidth: params.labelWidth === null || params.labelWidth === undefined ? null : params.labelWidth + 'px',
		},

		actions: params.actions ? { list: params.actions, cfg: params.actionsCfg } : null,

		...pick(params, [
			'tag',
			'execTag',
			'regList',
			'fieldId',
			'formId',
			'PFormID',
			'vModel',
			'defaultValue',
			'ifStaticOption',
			'options',
			'dynamicOptions',
			'vModel_t',
			'vModel_l',
			'vModel_c',
		]),
	};

	if (['el-checkbox-group', 'el-radio-group', 'el-select'].includes(params.tag)) {
		res.attrs.filterable = true;
		// 是否从远程获取数据,remote是要求从服务端获取数据源
		if (!params.ifStaticOption) {
			res.attrs.remote = true;
			if (!params.dynamicOptions || !params.dynamicOptions.url || !params.dynamicOptions.label || !params.dynamicOptions.value) {
				console.error('[Error][CustomPage]Cpn remote event config invalid: ', params);
				return null;
			}
		}
	}
	if (params.tag === 'el-cascader') {
		res.attrs.filterable = true;
		// 是否从远程获取数据,remote是要求从服务端获取数据源
		res.attrs.remote = true;
		if (!params.dynamicOptions || !params.dynamicOptions.url) {
			console.error('[Error][CustomPage]Cascader cpn remote event config invalid: ', params);
			return null;
		}
	}
	return res;
}
